package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.Medicine;
import com.hornsun.domain.dbo.Member;
import com.hornsun.domain.dbo.MemberExample;
import com.hornsun.repository.mapper.MemberMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.hornsun.util.DESEncryption.DATA_BASE;

/**
 * Created by 编译中... on 2018/2/2.
 */
@Repository
public class MemberRepository {
    @Resource
    private MemberMapper memberMapper;

    public void insertMember(Member member){
        if (member.getName()!=null){
            member.setName(DATA_BASE.encrypt(member.getName()));
        }
        if (member.getMobile()!=null){
            member.setMobile(DATA_BASE.encrypt(member.getMobile()));
        }
        if (member.getAddress()!=null){
            member.setAddress(DATA_BASE.encrypt(member.getAddress()));
        }
        int count = memberMapper.insertSelective(member);
        if (count!=1){
            throw new IllegalArgumentException("insert member failed by member:"+member);
        }
    }

    public void updateMember(Member member){

        if (member.getName()!=null){
            member.setName(DATA_BASE.encrypt(member.getName()));
        }
        if (member.getMobile()!=null){
            member.setMobile(DATA_BASE.encrypt(member.getMobile()));
        }
        if (member.getAddress()!=null){
            member.setAddress(DATA_BASE.encrypt(member.getAddress()));
        }
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andMemberGuidEqualTo(member.getMemberGuid());
        int count = memberMapper.updateByExampleSelective(member,memberExample);
        if (count!=1){
            throw new IllegalArgumentException("update member failed by member:"+member);
        }
    }


    public void deleteMember(String guid){
        Preconditions.checkNotNull(guid);
        int count = memberMapper.deleteByPrimaryKey(guid);
        if( count!=1){
            throw new IllegalArgumentException("delete member failed by guid:"+guid);
        }
    }

    public List<Member> getMemberByMobile(String mobile){
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        for(Member member : list){
            member.setMobile(DATA_BASE.decrypt(member.getMobile()));
            member.setAddress(DATA_BASE.decrypt(member.getAddress()));
            member.setName(DATA_BASE.decrypt(member.getName()));
        }
        return list;
    }
    public List<Member>  getMemberByName(String name){
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<Member> list = memberMapper.selectByExample(memberExample);
        for(Member member : list){

            member.setMobile(DATA_BASE.decrypt(member.getMobile()));
            member.setAddress(DATA_BASE.decrypt(member.getAddress()));
            member.setName(DATA_BASE.decrypt(member.getName()));
        }
        return list;
    }

}
